
import pygame,sys
from pygame.locals import *
from lib.Puzzle import PuzzlePiece

from os import path,listdir

class Loader:

    def __init__(self, lecture_name):
        self.sound_path   = "data/snd"                        
        self.lecture_path = "data/lectures/" + lecture_name            
        self.font_path    = "data/fnt"  
        self.img_path     = "data/img"      
        self.atributes_path = "data/lectures/" + lecture_name + "/atributes"  
        self.lecture_name = lecture_name
        self.path = "../"
        
    # Carga una imagen
    def load_image(self, filename):
        return pygame.image.load(path.join(self.img_path,filename))
    
    def load_solution(self):        
        return pygame.image.load(self.lecture_path + "/solution.png")

    # Craga el archivo de la lectura y retorna el texto
    def load_lecture(self):
        file = open(self.lecture_path + "/" + self.lecture_name + ".txt","r")
        text = "";
        try:
            for linea in file.readlines():
                text = text + linea;
            
            print text
            return text
        except:
            print "No cargo texto "
            return null;        

    # Devuelve una lista de los atributos del diagrama que debe armar
    def list_atributes(self):
        #list = listdir(self.atributes_path)
        atributes = []
       # 
        try:
            print path.join(self.atributes_path + "/attributes.txt")
            f = open(self.atributes_path + "/attributes.txt", "r")
            while True:
                linea = f.readline()
                print linea
                if linea:
                    attribute = linea.split(";")
                    print attribute
                    image = pygame.image.load(self.atributes_path + "/" + attribute[0])                    
                    points = attribute[1].split(",")
                    print len(points)
                    tupla = []
                    i = 0
                    while i < len(points):                        
                        px = int(points[i])
                        i = i + 1
                        py = int(points[i])
                        tupla.append((px,py))
                        print tupla
                        i = i + 1
                        
                    
                    atributes.append(PuzzlePiece(image,tupla))
                    print "Creando atributo"
                else:
                    break
        except:
            print " No fue posible crear los atributos"
                    
        print len(atributes) 
        return(atributes)
    
    # Carga el archivo de sonido
    def load_sound(self,filename):
        return pygame.mixer.Sound(path.join(self.sound_path,filename))
        
    # Carga la fuente para los textos
    # filename: archivo de fuente
    # size: Tamano de la fuente 
    def load_font(self,filename,size):
        return pygame.font.Font(path.join(self.font_path,filename),size)

